class Solution {
    int pre[], ino[];
    Map<Integer,Integer> map = new HashMap<>();
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        pre = preorder;
        ino = inorder;
        // 构造中序数组的map映射
        for(int i=0; i<inorder.length; i++)
            map.put(inorder[i],i);
        return build(0,pre.length-1,0,ino.length-1);
    }
    public TreeNode build(int l1,int r1,int l2,int r2){
        if( l1>r1 || l2>r2 )
            return null;
        TreeNode root = new TreeNode(pre[l1]);
        int index = map.get(pre[l1]);
        root.left = build(l1+1,l1+index-l2,l2,index-1);
        root.right = build(l1+index-l2+1,r1,index+1,r2);
        return root;
    }
}